/**
 * MeetStartAction.java
 */
package com.prs.crm.action.meeting;

import java.io.File;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Set;

import com.opensymphony.xwork2.validator.annotations.RegexFieldValidator;
import com.prs.crm.context.AppContext;
import com.prs.crm.context.Constrains;
import com.prs.crm.context.FileUtil;
import com.prs.crm.context.ToolKits;
import com.prs.crm.domain.assets.AssetsRegister;
import com.prs.crm.domain.hr.Dept;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.meeting.MeetProperty;
import com.prs.crm.domain.meeting.MeetRoom;
import com.prs.crm.domain.meeting.MeetStart;
import com.prs.crm.domain.meeting.MeetSummary;
import com.prs.crm.domain.meeting.MyBooking;
import com.prs.crm.domain.oa.NewsReceive;
import com.prs.crm.service.hr.PersonService;
import com.prs.crm.service.meeting.MeetPropertyService;
import com.prs.crm.service.meeting.MeetRoomService;
import com.prs.crm.service.meeting.MeetStartService;
import com.prs.crm.service.meeting.MeetSummaryService;

/**
 * @author ZhuDongMei
 * @E-mail:1223984339@qq.com
 * @qq:1223984339
 * @version 创建时间：2011-3-16 上午11:12:28 简单说明
 */

public class MeetStartAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private MeetStart meetStart;
	private PaginationSupport pagination;
	private MeetStartService meetStartService;
	private File upload;
	private String uplodS;
	private String uploadFileName;
	private String reg;
	private Set<MeetRoom> meetRooms;
	private MeetRoomService meetRoomService;
	private Set<MeetProperty> meetPropertys;
	private MeetPropertyService meetPropertyService;
	private Dept dept;
	private Set<Dept> depts;
	private String receivePersonId;
	private List<Person> person;
	private PersonService personService;
	private List<Person> pers;
	private Integer[] meetWaitingIds;
	private MeetStart meetStarts;
	private Integer[] meetTempIds;
	List<MeetStart> meets;
	private Integer[] meetEndIds;
	private MeetSummary meetSummary;
	private MeetSummaryService meetSummaryService;
	public String name;
	public List<MeetSummary> meetSummarys;
	public Integer[] maintainIds;
	private Integer[] meetSummaryIds;
    
	public MeetSummary getMeetSummary() {
		return meetSummary;
	}

	public void setMeetSummary(MeetSummary meetSummary) {
		this.meetSummary = meetSummary;
	}
    
	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public Integer[] getMeetSummaryIds() {
		return meetSummaryIds;
	}

	public void setMeetSummaryIds(Integer[] meetSummaryIds) {
		this.meetSummaryIds = meetSummaryIds;
	}
	public void setReceivePersonId(String receivePersonId) {
		this.receivePersonId = receivePersonId;
	}
	public List<MeetStart> getMeets() {
		return meets;
	}

	public void setMeets(List<MeetStart> meets) {
		this.meets = meets;
	}

	public Integer[] getMaintainIds() {
		return maintainIds;
	}

	public void setMaintainIds(Integer[] maintainIds) {
		this.maintainIds = maintainIds;
	}

	public List<MeetSummary> getMeetSummarys() {
		return meetSummarys;
	}

	public void setMeetSummarys(List<MeetSummary> meetSummarys) {
		this.meetSummarys = meetSummarys;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public MeetSummaryService getMeetSummaryService() {
		return meetSummaryService;
	}

	public void setMeetSummaryService(MeetSummaryService meetSummaryService) {
		this.meetSummaryService = meetSummaryService;
	}

	public Integer[] getMeetEndIds() {
		return meetEndIds;
	}

	public void setMeetEndIds(Integer[] meetEndIds) {
		this.meetEndIds = meetEndIds;
	}

	public Integer[] getMeetTempIds() {
		return meetTempIds;
	}

	public void setMeetTempIds(Integer[] meetTempIds) {
		this.meetTempIds = meetTempIds;
	}

	public MeetStart getMeetStarts() {
		return meetStarts;
	}

	public void setMeetStarts(MeetStart meetStarts) {
		this.meetStarts = meetStarts;
	}

	public Integer[] getMeetWaitingIds() {
		return meetWaitingIds;
	}

	public void setMeetWaitingIds(Integer[] meetWaitingIds) {
		this.meetWaitingIds = meetWaitingIds;
	}

	public List<Person> getPers() {
		return pers;
	}

	public void setPers(List<Person> pers) {
		this.pers = pers;
	}

	public List<Person> getPerson() {
		return person;
	}

	public void setPerson(List<Person> person) {
		this.person = person;
	}

	public PersonService getPersonService() {
		return personService;
	}

	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}

	@Action("listMeetStartByReg")
	public String listMeetStartByReg() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				MeetStart.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		if (reg != null) {
			if (!"all".equals(reg)) {
				reg = reg.split(",")[0];
				if (ToolKits.isInteger(reg)) {
					criteria.createAlias("", "");
				}
			}
		}
		this.setPagination(this.getMeetStartService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editMeetStart")
	public String editMeetStart() {
		this.person = this.getPersonService().getAllPersons();
		this.pers = this.getPersonService().getAllPersons();
		return SUCCESS;
	}

	@Action("viewMeetStart")
	public String viewMeetStart() {
		return SUCCESS;
	}

	@Action("listMeetWaiting")
	public String listMeetting() {
		meets = this.getMeetStartService().getAllMeetStarts();
		for (int i = 0; i < meets.size(); i++) {
			if (new Date().before(meets.get(i).getEndTime())
					&& "2".equals(meets.get(i).getState())) {
				meets.get(i).setState(Constrains.MEET_WAITING);
				this.getMeetStartService().save(meets.get(i));
			}
		}
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				MeetStart.class).add(
				Restrictions.eq("state", Constrains.MEET_WAITING));
		criteria.add(Restrictions.or(Restrictions.like("peopleId", "%"+AppContext.getAccountContext().currentUser().getId()+"%"), Restrictions.eq("recorder.id", AppContext.getAccountContext().currentUser().getId())));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getMeetStartService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("listMeetEnd")
	public String listMeetEnd() {
		meets = this.getMeetStartService().getAllMeetStarts();
		for (int i = 0; i < meets.size(); i++) {
			if (new Date().after(meets.get(i).getEndTime())
					&& !"1".equals(meets.get(i).getState())) {
				meets.get(i).setState(Constrains.MEET_END);
				this.getMeetStartService().save(meets.get(i));
			}
		}
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				MeetStart.class).add(
				Restrictions.eq("state", Constrains.MEET_END));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getMeetStartService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action(value = "sendMeet", results = {
			@Result(name = "success", type = "redirect", location = "listMeetWaiting"),
			@Result(name = "none", type = "redirect", location = "editMeetStart"),
			@Result(name = "error", type = "redirect", location = "listMeetEnd") })
	public String sendMeet() {
//		System.out.println("uplodS:"+uplodS);
//		upload=new File(uplodS);
//		if(new Date().after(meetStart.getEndTime())) {
//			meetStart.setState(Constrains.MEET_END);
//			if(upload!=null){
//				this.getMeetStartService().saveMeetStarts(meetStart, upload, uploadFileName);
//			}else{
//			this.getMeetStartService().save(meetStart);
//			}
//			return ERROR;
//			
//		} else {
//			meetStart.setState(Constrains.MEET_WAITING);
//			if(upload!=null){
//				this.getMeetStartService().saveMeetStarts(meetStart, upload, uploadFileName);
//			}else{
//			this.getMeetStartService().save(meetStart);
//			}
//			return SUCCESS;
//		}
		if(new Date().after(meetStart.getEndTime())) {
			meetStart.setState(Constrains.MEET_END);
			if(upload!=null){
				this.meetStart.setAttach(FileUtil.getInstance().createAttach(upload, uploadFileName));
			}
			meetStart.setMeetRoom(meetStart.getMeetRoom());
			this.getMeetStartService().save(meetStart);	
			return ERROR;
			
		} else {
			meetStart.setState(Constrains.MEET_WAITING);
			if(upload!=null){
				this.meetStart.setAttach(FileUtil.getInstance().createAttach(upload, uploadFileName));
			}
			meetStart.setMeetRoom(meetStart.getMeetRoom());
			this.getMeetStartService().save(meetStart);
			return SUCCESS;
		}
	}
	
	@Action(value = "saveMeetStart", results = {
			@Result(name = "success", type = "redirect", location = "listMeetTemp"),
			@Result(name = "error", type = "chain", location = "editMeetStart") })
	public String saveMeetStart() {
		if(upload!=null){
			this.getMeetStartService().saveMeetStarts(meetStart, upload, uploadFileName);
		}else{
		if ("".equals(meetStart.getDept().getName().trim())) {
			addActionError("主办单位不能为空！");
			return ERROR;
		}
		if ("".equals(meetStart.getGrade().trim())) {
			addActionError("重要级别不能为空！");
			return ERROR;
		}
		if ("".equals(meetStart.getPerson().getResume().getName().trim())) {
			addActionError("主持人不能为空！");
			return ERROR;
		}
		if ("".equals(meetStart.getEndTime())) {
			addActionError("结束时间不能为空！");
			return ERROR;
		}
		 else {
			meetStart.setState(Constrains.MEET_TEMP);
			this.meetStartService.save(meetStart);
			
		}
		}
		return SUCCESS;
	}

	@Action(value = "removeMeetTemp", results = {
			@Result(name = "success", type = "redirect", location = "listMeetTemp"),
			@Result(name = "error", type = "chain", location = "listMeetTemp") })
	public String removeMeetTemp() {
		if (meetTempIds == null) {
			this.addActionError("请选择数据行");
			return ERROR;
		} else {
			this.getMeetStartService().batchRemove(this.getMeetTempIds());
			return SUCCESS;
		}
	}

	@Action("listMeetTemp")
	public String listMeetTemp() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				MeetStart.class).add(
				Restrictions.eq("state", Constrains.MEET_TEMP));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getMeetStartService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("viewMeet")
	public String viewMeet() {
		if (meetWaitingIds != null) {
			this.meetStarts = this.getMeetStartService().get(meetWaitingIds[0]);
		}
		return SUCCESS;
	}

	@Action("editSummary")
	public String editSummary() {
		this.meetStart = this.getMeetStartService().get(meetEndIds[0]);
		meetSummary = this.meetStart.getMeetSummary();
		return SUCCESS;
	}
	@Action(value = "saveSummary", results = {
			@Result(name = "success", type = "redirect", location = "listMeetEnd"),
			@Result(name = "error", type = "chain", location = "editSummary") })
	public String saveSummary() {
		if ("".equals(meetSummary.getContext().trim())) {
			this.addActionError("纪要内容不能为空");
			return ERROR;
		} else {
			meetStart.setMeetSummary(meetSummary);
			meetSummary.setName(meetStart.getName());
			meetSummary.setEndTime(meetStart.getEndTime());
			meetSummary.setDept(meetStart.getDept());
			if(upload!=null){
				this.getMeetSummaryService().saveMeetSummarys(meetSummary, upload, uploadFileName);
			}else{
			this.getMeetSummaryService().save(meetSummary);
			
		}
			return SUCCESS;
		}
	} 
	@Action(value = "viewSummary", results = {
			@Result(name = "error", type = "chain", location = "listMeetEnd") })
	public String viewSummary() {
			this.meetStart = this.getMeetStartService().get(meetEndIds[0]);
			meetSummary = this.meetStart.getMeetSummary();
			if (meetSummary == null) {
				this.addActionError("当前会议没有纪要！请先创建纪要");
				return ERROR;
			} else {
				meetSummary = this.meetStart.getMeetSummary();			
				return SUCCESS;
			}
	}
//	@Action("viewSummarys")
	@Action(value = "viewSummarys", results = {
		@Result(name = "error", type = "chain", location = "listMeetMaintain") })
	public String viewSummarys() {
			this.meetStart = this.getMeetStartService().get(maintainIds[0]);
			meetSummary = this.meetStart.getMeetSummary();
			if (meetSummary == null) {
				this.addActionError("当前会议没有纪要！请先创建纪要");
				return ERROR;
			} else {
				meetSummary = this.meetStart.getMeetSummary();
				return SUCCESS;
			}
	}

	@Action("listMeetMaintain")
	public String listMeetMaintain() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				MeetStart.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getMeetStartService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("listMeetMaintains")
	public String listMeetMaintains() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				MeetStart.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getMeetStartService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action(value = "meetCancel", results = {
			@Result(name = "success", type = "redirect", location = "listMeetMaintains"),
			@Result(name = "error", type = "chain", location = "listMeetMaintains") })
	public String meetCancel() {
		if (maintainIds != null) {
			this.meetStart = this.getMeetStartService().get(maintainIds[0]);
			if ("3".equals(meetStart.getState())) {
				this.addActionError("此会议已结束！");
				return ERROR;
			} else if ("4".equals(meetStart.getState())) {
				this.addActionError("此会议已删除！");
				return ERROR;
			} else if ("1".equals(meetStart.getState())) {
				this.addActionError("此会议处于暂存状态！");
				return ERROR;
			} else if ("5".equals(meetStart.getState())) {
				this.addActionError("此会议已取消！");
				return ERROR;
			} else {
				meetStart.setState("5");
				this.getMeetStartService().save(meetStart);
			}
		}
		return SUCCESS;
	}

	@Action(value = "updateMeet", results = {
			@Result(name = "success", type = "redirect", location = "editMeetStart?meetStart=${meetStart.getId()}"),
			@Result(name = "error", type = "chain", location = "listMeetMaintain") })
	public String updateMeet() {
		if (maintainIds != null) {
			this.meetStart = this.getMeetStartService().get(maintainIds[0]);
			if ("4".equals(meetStart.getState())) {
				this.addActionError("此会议已删除!");
				return ERROR;
			}
		}
		this.person = this.getPersonService().getAllPersons();
		this.pers = this.getPersonService().getAllPersons();
		return SUCCESS;
	}

	@Action(value = "removeMeet", results = {
			@Result(name = "success", type = "redirect", location = "listMeetMaintains"),
			@Result(name = "error", type = "chain", location = "listMeetMaintains") })
	public String removeMeet() {
		if (maintainIds != null) {
			this.meetStart = this.getMeetStartService().get(maintainIds[0]);
			if ("1".equals(meetStart.getState())) {
				this.addActionError("此会议处于暂存状态。。不能进行删除！");
				return ERROR;
			} else {
				meetStart.setState("4");
				this.getMeetStartService().save(meetStart);
			}
		}
		return SUCCESS;
	}

	@Action(value = "cancelDelete", results = {
			@Result(name = "success", type = "redirect", location = "listMeetMaintains"),
			@Result(name = "error", type = "chain", location = "listMeetMaintains") })
	public String cancelDelete() {
		if (maintainIds != null) {
			this.meetStart = this.getMeetStartService().get(maintainIds[0]);
			if (!"4".equals(meetStart.getState())) {
				this.addActionError("此会议未处于删除状态！");
				return ERROR;
			} else {
				if (new Date().before(meetStart.getEndTime())) {
					meetStart.setState("2");
					this.getMeetStartService().save(meetStart);
				} else {
					meetStart.setState("3");
					this.getMeetStartService().save(meetStart);
				}
				return SUCCESS;
			}
		}
		return SUCCESS;
	}
	
    @Action("listMeetSummary")
    public String listMeetSummary(){
    	DetachedCriteria criteria = this.getPaginationHelper().createQuery(MeetSummary.class);
    	pagination = this.getPaginationHelper().prepare(pagination);
    	this.setPagination(this.getMeetSummaryService().listByPage(criteria, pagination));
    	return SUCCESS;
    	}
	public String getReceivePersonId() {
		return receivePersonId;
	}
    @Action("viewMeetSummary")
    public String viewMeetSummary(){
    	return SUCCESS;
    }
    @Action(value = "saveMeetSummary", results = {
			@Result(name = "success", type = "redirect", location = "listMeetSummary"),
			@Result(name = "error", type = "chain", location = "editMeetSummary") })
    public String saveMeetSummary(){
    	
    	if (meetSummary.getContext().length()==0) {
			this.addActionError("纪要内容不能为空");
			return ERROR;
		}else{
			meetSummary.setEndTime(new Date());
			if(upload!=null){
				this.meetSummary.setAttach(FileUtil.getInstance().createAttach(upload, uploadFileName));
			}
			this.getMeetSummaryService().save(meetSummary);
		}
		return SUCCESS;
    }
    @Action("editMeetSummary")
	public String editMeetSummary() {
    	if(meetSummaryIds!=null){
		this.meetSummary = this.getMeetSummaryService().get(meetSummaryIds[0]);
    	}
		return SUCCESS;
	}
    @Action(value = "deleteSummary", results = {
			@Result(name = "success", type = "redirect", location = "listMeetSummary"),
			@Result(name = "error", type = "chain", location = "listMeetSummary") })
    public String deleteSummary(){
    	if(meetSummaryIds==null){
    		this.addActionError("请选择数据行!");
    		return ERROR;
    	}else{
    		this.getMeetSummaryService().batchRemove(this.getMeetSummaryIds());
    		return SUCCESS;
    	}
    }
    @Action(value = "deleteSummarys", results = {
			@Result(name = "success", type = "redirect", location = "listMeetSummary"),
			@Result(name = "error", type = "chain", location = "listMeetSummary") })
    public String deleteSummarys(){
    	if(meetSummaryIds==null){
    		this.addActionError("请选择数据行!");
    		return ERROR;
    	}else{
    		for(int i=0;i<meetSummaryIds.length;i++){
    			this.meetStart = this.getMeetStartService().getMeetStartByMeetSummary(this.getMeetSummaryService().getMeetSummaryById(meetSummaryIds[i]));
    			if(meetStart!=null){
    				meetStart.setMeetSummary(null);
    				this.getMeetSummaryService().remove(this.getMeetSummaryService().getMeetSummaryById(meetSummaryIds[i]));
    				this.getMeetStartService().remove(meetStart);
    			}else{
    				this.getMeetSummaryService().remove(this.getMeetSummaryService().getMeetSummaryById(meetSummaryIds[i]));
    			}
    		}
    		return SUCCESS;
    	}
    }
    
	public MeetStart getMeetStart() {
		return meetStart;
	}

	public void setMeetStart(MeetStart meetStart) {
		this.meetStart = meetStart;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public MeetStartService getMeetStartService() {
		return meetStartService;
	}

	public void setMeetStartService(MeetStartService meetStartService) {
		this.meetStartService = meetStartService;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUplodS() {
		return uplodS;
	}

	public void setUplodS(String uplodS) {
		this.uplodS = uplodS;
	}

	public String getReg() {
		return reg;
	}

	public void setReg(String reg) {
		this.reg = reg;
	}

	public Set<MeetRoom> getMeetRooms() {
		return meetRooms;
	}

	public void setMeetRooms(Set<MeetRoom> meetRooms) {
		this.meetRooms = meetRooms;
	}

	public MeetRoomService getMeetRoomService() {
		return meetRoomService;
	}

	public void setMeetRoomService(MeetRoomService meetRoomService) {
		this.meetRoomService = meetRoomService;
	}

	public Set<MeetProperty> getMeetPropertys() {
		return meetPropertys;
	}

	public void setMeetPropertys(Set<MeetProperty> meetPropertys) {
		this.meetPropertys = meetPropertys;
	}

	public MeetPropertyService getMeetPropertyService() {
		return meetPropertyService;
	}

	public void setMeetPropertyService(MeetPropertyService meetPropertyService) {
		this.meetPropertyService = meetPropertyService;
	}

	public Dept getDept() {
		return dept;
	}

	public void setDept(Dept dept) {
		this.dept = dept;
	}

	public Set<Dept> getDepts() {
		return depts;
	}

	public void setDepts(Set<Dept> depts) {
		this.depts = depts;
	}
}
